<template>

    <div class="goods-list">

        <!-- 标签式导航 -->
        <!-- <router-link class="goods-item" v-for="item in goodsList" :key="item.id" :to="'/home/goodsInfo/' + item.id" tag="div">
            <img :src="item.imgUrl">
            <h1 class="goods-title">{{ item.title }}</h1>

            <div class="goods-info">
                <p class="goods-price">
                    <span class="goods-now">{{ item.sellPrice }}</span>
                    <span class="goods-old">{{ item.marketPrice }}</span>
                </p>
                <p class="goods-sell">
                    <span class="goods-now">热卖中</span>
                    <span class="goods-old">剩余:{{ item.stockQuantity }}件</span>
                </p>
            </div>
        </router-link> -->

        <!-- 使用JS编程式导航 -->
        <div class="goods-item" v-for="item in goodsList" :key="item.id" @click="getDetail(item.id)">
            <img :src="item.imgUrl">
            <h1 class="goods-title">{{ item.title }}</h1>

            <div class="goods-info">
                <p class="goods-price">
                    <span class="goods-now">￥{{ item.sellPrice }}</span>
                    <span class="goods-old">￥{{ item.marketPrice }}</span>
                </p>
                <p class="goods-sell">
                    <span class="goods-now">热卖中</span>
                    <span class="goods-old">剩余:{{ item.stockQuantity }}件</span>
                </p>
            </div>
        </div>

        <mt-button type="danger" size="large" @click="more">加载更多</mt-button>
    </div>
</template>

<script>
//导入mui中Toast组件
import { Toast } from 'mint-ui';
export default {
    data() {
        return {
            pageIndex: 1,
            goodsList: []
        }
    },
    methods: {
        getGoodsList() {
            this.$http.get('vue/getGoodsList?pageIndex=' + this.pageIndex).then(result => {
                if (result.body.status === 0) {
                    let list = result.body.message
                    //数组拼接 每次获取新数据时，均为老数据拼接上新数据
                    this.goodsList = this.goodsList.concat(list);
                    if (this.pageIndex != 1 && list.length === 0) {
                        Toast('刷到底了！')
                    }
                } else {
                    Toast('获取商品列表失败')
                }
            });
        },
        more() {
            this.pageIndex ++
            this.getGoodsList()
        },
        getDetail(id) {
            //使用JS的进行路由导航
            //注意要区分 this.$router 与 this.$route 的区别
            //this.$route是路由【参数对象】，所有路由中的参数 params query都属于他
            //this.$router是一个路由【导航对象】，用它可以方便的使用JS代码实现路由的前进、后退、跳转到新的URL

            //方式1.传递字符串
            //this.$router.push("/home/goodsInfo/" + id);
            //方式2.传递对象 如果使用path 那么params属性中的内容会被忽略 
            //如果使用问号传参例如/home/goodsInfo?id=1 使用this.$router.push({path: '/home/goodsInfo', query: {id: id}});
            //this.$router.push({path: '/home/goodsInfo/' + id});
            //方式3.传递一个命名的路由 之前的router.js中都没有定义路由的name
            this.$router.push({name: 'goodsInfo', params: {id: id}})
        }
    },
    created() {
        this.getGoodsList();
    }
}
</script>

<style lang="scss" scoped>
    .goods-list {
        display: flex;
        flex-wrap: wrap;
        padding: 7px;
        justify-content: space-between;


        .goods-item {
            width: 49%;
            border: 1px solid #ccc;
            box-shadow: 0 0 8px #ccc;
            margin: 3px 0;
            padding: 2px;
            min-height: 260px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            img {
                width: 100%;
            }
            .goods-title {
                font-size: 14px;
                margin: 0px;
            }

            .goods-info {
                background-color: #eee;
                p {
                    margin: 0;
                    padding: 5px;
                }
                .goods-price {
                    .goods-now {
                        color: red;
                        font-weight: bold;
                        font-size: 16px;
                    }

                    .goods-old {
                        text-decoration: line-through;
                        font-size: 12px;
                        margin-left: 10px;
                    }
                }

                .goods-sell {
                    display: flex;
                    justify-content: space-between;
                    font-size: 13px;
                }
            }
        }
    }
</style>